package com.yljk.homedoctor;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.CommonUtils;

public class HomePopWindowManager {


    private static HomePopWindowManager homePopWindowManager;

    private Context context;

    public static synchronized HomePopWindowManager getInstance() {
        if (homePopWindowManager == null) {
            homePopWindowManager = new HomePopWindowManager();
        }
        return homePopWindowManager;
    }


    public void init(Context context) {
        this.context = context;

        initLiveSign(context);

    }
    /**
     * 温馨提示
     */
    private TextView liveCentent;
    private TextView liveCancel;
    private TextView liveSure;
    private TextView liveTitle;
    private PopupWindow livePop;
    private View liveView;

    //温馨提示
    public void initLiveSign(Context context) {
        livePop = new PopupWindow();
        liveView = LayoutInflater.from(context).inflate(R.layout.home_fragment_pop_live, null);
        liveCentent = (TextView) liveView.findViewById(R.id.home_fragment_pop_live_centent);
        liveCancel = (TextView) liveView.findViewById(R.id.home_fragment_pop_live_cancel);
        liveSure = (TextView) liveView.findViewById(R.id.home_fragment_pop_live_sure);
        liveTitle = (TextView) liveView.findViewById(R.id.home_fragment_pop_live_title);
        livePop.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        livePop.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        livePop.setContentView(liveView);
        livePop.setOutsideTouchable(false);
        livePop.setFocusable(false);
        livePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                CommonUtils.setBackgroundAlpha((Activity) context,1.0f);
            }
        });
        liveCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                livePop.dismiss();
            }
        });
    }

    public void setLiveliveTitle(String title){
        if (liveTitle == null){
            ALog.e("HomePopWindowManager-setLiveliveTitle()","liveTitle is null");
            return;
        }else if (title == null || TextUtils.isEmpty(title)){
            ALog.e("HomePopWindowManager-setLiveliveTitle()","title is null");
            return;
        }else{
            liveTitle.setText(title);
        }
    }
    public void setLiveliveContent(String content){
        if (liveCentent == null){
            ALog.e("HomePopWindowManager-setLiveliveContent()","liveCentent is null");
            return;
        }else if (content == null || TextUtils.isEmpty(content)){
            ALog.e("HomePopWindowManager-setLiveliveContent()","content is null");
            return;
        }else{
            liveCentent.setText(content);
        }
    }
    public void setLiveliveCancel(String cancel){
        if (liveCancel == null){
            ALog.e("HomePopWindowManager-setLiveliveCancel()","liveCancel is null");
            return;
        }else if (cancel == null || TextUtils.isEmpty(cancel)){
            ALog.e("HomePopWindowManager-setLiveliveCancel()","cancel is null");
            return;
        }else{
            liveCancel.setText(cancel);
        }
    }
    public void setLiveliveSure(String sure){
        if (liveSure == null){
            ALog.e("HomePopWindowManager-setLiveliveSure()","liveSure is null");
            return;
        }else if (sure == null || TextUtils.isEmpty(sure)){
            ALog.e("HomePopWindowManager-setLiveliveSure()","sure is null");
            return;
        }else{
            liveSure.setText(sure);
        }

    }

    /**
     * 显示肿瘤未开通权限须知
     * @param context
     */
    public void showLiveSign(Context context){
        if (context instanceof Activity && livePop!=null){
            Activity activity = (Activity) this.context;
            livePop.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            CommonUtils.setBackgroundAlpha(activity,0.3f);
        }
    }

    public void setliveSureClick(View.OnClickListener clickListener){
        if (clickListener == null){
            ALog.e("HomePopWindowManager-setLiveSureClick()","clickListener is null");
            return;
        }
        ClickUtils.setFastOnClickListener(liveSure,clickListener);
    }

    public void setLiveCancelClick(View.OnClickListener clickListener){
        if (clickListener == null){
            ALog.e("HomePopWindowManager-setLiveCancelClick()","clickListener is null");
            return;
        }
        clickListener.onClick(liveCancel);
    }

}
